package com.dang.web.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dang.util.ImageUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
@WebServlet("/checkCode.action")
public class SafeCodeServlet extends HttpServlet{
	private static final long serialVersionUID = -2971417344472546855L;
	
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
		doPost(req,resp);	
	}
	
	protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
		HttpSession session=req.getSession();
		Map<String,BufferedImage> map=ImageUtil.createImage();
		String key=map.keySet().iterator().next();
		session.setAttribute("checkCode", key);
		BufferedImage iamge=map.get(key);
		ServletOutputStream out=resp.getOutputStream();
		JPEGImageEncoder imageEncoder=JPEGCodec.createJPEGEncoder(out);
		imageEncoder.encode(iamge);
		out.flush();
	}
}
